//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Rounding.h"
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CRounding::CRounding (Int32 precision, Microsoft::FSharp::Core::FSharpOption<QL::Math::Rounding::TypeEnum>^ type, Microsoft::FSharp::Core::FSharpOption<Int32>^ digit) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phRounding = NULL;
#endif
        QuantLib::Integer _precision = (QuantLib::Integer)ValueHelper::Convert (precision); //d
        QuantLib::Rounding::Type _type = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Math::Rounding::TypeEnum>::IsSome::get (type) ? (QuantLib::Rounding::Type)type->Value : QuantLib::Rounding::Type::Closest); //10
        QuantLib::Integer _digit = 
            (Microsoft::FSharp::Core::FSharpOption<Int32>::IsSome::get (digit) ? (QuantLib::Integer)ValueHelper::Convert (digit->Value) : 5); //4
        _ppRounding = new boost::shared_ptr<QuantLib::Rounding> (new QuantLib::Rounding ( _precision,  _type,  _digit ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::CRounding::CRounding () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phRounding = NULL;
#endif
        _ppRounding = new boost::shared_ptr<QuantLib::Rounding> (new QuantLib::Rounding ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::CRounding::CRounding (boost::shared_ptr<QuantLib::Rounding>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRounding = NULL;
#endif
	_ppRounding = &childNative;
    
}
Cephei::QL::Math::CRounding::CRounding (QuantLib::Rounding& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phRounding = NULL;
#endif
	_ppRounding = new boost::shared_ptr<QuantLib::Rounding> (&childNative);
    
    _RoundingOwner = owner;
    
}

Cephei::QL::Math::CRounding::CRounding (CRounding^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRounding = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRounding = new boost::shared_ptr<QuantLib::Rounding> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CRounding::CRounding (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRounding = NULL;
#endif
	if (!t->IsSubclassOf(CRounding::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CRounding::CRounding (QuantLib::Handle<QuantLib::Rounding>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRounding = &childNative;
	_ppRounding = &static_cast<boost::shared_ptr<QuantLib::Rounding>>(childNative.currentLink());
    
    _RoundingOwner = owner;
}
Cephei::QL::Math::CRounding::CRounding (QuantLib::Handle<QuantLib::Rounding> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRounding = &childNative;
	_ppRounding = &static_cast<boost::shared_ptr<QuantLib::Rounding>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CRounding::CRounding (QuantLib::Rounding childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRounding = NULL;
#endif
	_ppRounding = new boost::shared_ptr<QuantLib::Rounding> (new QuantLib::Rounding (childNative));
    
}
#endif

Cephei::QL::Math::CRounding::~CRounding ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppRounding != NULL)
    {
	    delete _ppRounding;
        _ppRounding = NULL;
    }
}
Cephei::QL::Math::CRounding::!CRounding ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppRounding != NULL)
    {
	    delete _ppRounding;
    }
}
QuantLib::Rounding& Cephei::QL::Math::CRounding::GetReference ()
{
    if (_ppRounding == NULL) throw REFNEW NativeNullException ();
	return **_ppRounding;
}
boost::shared_ptr<QuantLib::Rounding>& Cephei::QL::Math::CRounding::GetShared ()
{
    if (_ppRounding == NULL) throw REFNEW NativeNullException ();
	return *_ppRounding;
}
QuantLib::Rounding* Cephei::QL::Math::CRounding::GetPointer ()
{
    if (_ppRounding == NULL) throw REFNEW NativeNullException ();
	return &**_ppRounding;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Rounding>& Cephei::QL::Math::CRounding::GetHandle ()
{
	if (_phRounding == NULL)
	{
		_phRounding = new Handle<QuantLib::Rounding> (*_ppRounding);
	}
	return *_phRounding;
}
#endif
bool Cephei::QL::Math::CRounding::HasNative () 
{
	return (_ppRounding != NULL);
}

Int32 Cephei::QL::Math::CRounding::Precision::get ()
{
    try
    {
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppRounding)->precision ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Int32 Cephei::QL::Math::CRounding::RoundingDigit::get ()
{
    try
    {
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppRounding)->roundingDigit ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Math::Rounding::TypeEnum Cephei::QL::Math::CRounding::Type::get ()
{
    try
    {
    	QuantLib::Rounding::Type _rv = (QuantLib::Rounding::Type)(*_ppRounding)->type ( );   
        QL::Math::Rounding::TypeEnum _nrv = (QL::Math::Rounding::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::IRounding^ Cephei::QL::Math::CRounding_Factory::Create (Int32 precision, Microsoft::FSharp::Core::FSharpOption<QL::Math::Rounding::TypeEnum>^ type, Microsoft::FSharp::Core::FSharpOption<Int32>^ digit)
{
    return REFNEW CRounding ( precision,  type,  digit);
}
Cephei::QL::Math::IRounding^ Cephei::QL::Math::CRounding_Factory::Create ()
{
    return REFNEW CRounding ();
}
